Telegram Group & Telegram Channel
Note #7 О стилистике Decode//UnMarshal 🐣

Недавно в твиттере возник вопрос, что лучше писать:

// Option A
var v T
v.Decode(someData)
Или так:
Option B
func (t *T) Decode(data []byte) {
// decode data into *t
}

На самом деле все зависит от контекста, а именно вариант В) или использование метода вместо функции - позволяет типам удовлетворять интерфейсам, т.е другими словами, если у вас есть где-то type Decoder interface{ Decode([]byte) error },
Если такого типа нет, оба варианта подойдут и как все знают на вкус и цвет фломастеры разные, хотя второй вариант более универсальный кмк.

Пару ссылок:
- Интересный gist о том как можно приводить типы между источниками
- Дока о том, что Decode - это обычно выражает Unmarshal BinaryUnmarshaler



tg-me.com/golang_for_two/30
Create:
Last Update:

Note #7 О стилистике Decode//UnMarshal 🐣

Недавно в твиттере возник вопрос, что лучше писать:

// Option A
var v T
v.Decode(someData)
Или так:
Option B
func (t *T) Decode(data []byte) {
// decode data into *t
}

На самом деле все зависит от контекста, а именно вариант В) или использование метода вместо функции - позволяет типам удовлетворять интерфейсам, т.е другими словами, если у вас есть где-то type Decoder interface{ Decode([]byte) error },
Если такого типа нет, оба варианта подойдут и как все знают на вкус и цвет фломастеры разные, хотя второй вариант более универсальный кмк.

Пару ссылок:
- Интересный gist о том как можно приводить типы между источниками
- Дока о том, что Decode - это обычно выражает Unmarshal BinaryUnmarshaler

BY 🇺🇦 Go for two :)


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/golang_for_two/30

View MORE
Open in Telegram


🇺🇦 Go на двоих Telegram | DID YOU KNOW?

Date: |

Traders also expressed uncertainty about the situation with China Evergrande, as the indebted property company has not provided clarification about a key interest payment.In economic news, the Commerce Department reported an unexpected increase in U.S. new home sales in August.Crude oil prices climbed Friday and front-month WTI oil futures contracts saw gains for a fifth straight week amid tighter supplies. West Texas Intermediate Crude oil futures for November rose $0.68 or 0.9 percent at 73.98 a barrel. WTI Crude futures gained 2.8 percent for the week.

Start with a fresh view of investing strategy. The combination of risks and fads this quarter looks to be topping. That means the future is ready to move in.Likely, there will not be a wholesale shift. Company actions will aim to benefit from economic growth, inflationary pressures and a return of market-determined interest rates. In turn, all of that should drive the stock market and investment returns higher.

🇺🇦 Go на двоих from sg


Telegram 🇺🇦 Go for two :)
FROM USA